package com.tzstcl.sys.webSocket.ctrl;

import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.sys.webSocket.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;

/**
 * WebSocket功能测试
 */
@Controller
public class WebSocketCtrl extends BaseCtrl {

    @Autowired
    WebSocketService webSocketService;

    /**
     * 发送全局消息
     * @param message
     * @return
     */
    @MessageMapping("/global")
    @SendTo(value = "/channel/global")
    public String global(String message) {
        return message;
    }

    /**
     * 发送分组消息
     * @param groupId,message
     * @return
     */
    @MessageMapping("/group/{groupId}")
    public void group(@DestinationVariable("groupId") String groupId, String message) {
        webSocketService.sendGroupMessage(groupId, message);
    }

    /**
     * 发送点对点消息
     * @param userId,message
     * @return
     */
    @MessageMapping("/user/{userId}")
    @SendToUser(value = "/channel/user")
    public String user(@DestinationVariable("userId") String userId, String message) {
        webSocketService.sendPointMessage(userId, message);
        return "success";
    }

    /**
     * 接收临时订阅, 返回一次性资源(模拟正常http请求)
     * @param message @Header注解从请求头中读取参数
     * @return
     */
    @SubscribeMapping("/temporary")
    public String temporary(@Header("message") String message) {
        return message;
    }

}
